#include "widget.h"
#include "ui_widget.h"
#include <QPainter>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    QPixmap background(":/background");

    int nLeftWidth = 144;
    int nBottomHeight = 24;
    int nTopHeight = 67;

    // 分别计算左、上、右、下的区域
    QRect left(0, 100, nLeftWidth, 100);
    QRect right(background.width() - nLeftWidth, 100, nLeftWidth, 100);
    QRect leftTop(0, 0, nLeftWidth, nTopHeight);
    QRect rightTop(background.width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
    QRect top(150, 0, 150, nTopHeight);
    QRect leftBottom(0, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect rightBottom(background.width() - nLeftWidth, background.height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect bottom(150, background.height() - nBottomHeight, 100, nBottomHeight);
    QRect center(300, 300, 100, 100);

    QRect leftRect(0, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
    QRect rightRect(this->width() - nLeftWidth, nTopHeight, nLeftWidth, this->height() - nTopHeight - nBottomHeight);
    QRect leftTopRect(0, 0, nLeftWidth, nTopHeight);
    QRect rightTopRect(this->width() - nLeftWidth, 0, nLeftWidth, nTopHeight);
    QRect topRect(nLeftWidth, 0, this->width() - nLeftWidth*2, nTopHeight);
    QRect leftBottomRect(0, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect righttBottomRect(this->width() - nLeftWidth, this->height() - nBottomHeight, nLeftWidth, nBottomHeight);
    QRect bottomRect(nLeftWidth, this->height() - nBottomHeight, this->width() - nLeftWidth*2, nBottomHeight);
    QRect centerRect(nLeftWidth, nTopHeight, this->width() - nLeftWidth*2, this->height() - nTopHeight - nBottomHeight);

    // 绘制图片
    painter.drawPixmap(topRect, background, top);
    painter.drawPixmap(leftRect, background, left);
    painter.drawPixmap(rightRect, background, right);
    painter.drawPixmap(rightTopRect, background, rightTop);
    painter.drawPixmap(leftTopRect, background, leftTop);
    painter.drawPixmap(leftBottomRect, background, leftBottom);
    painter.drawPixmap(righttBottomRect, background, rightBottom);
    painter.drawPixmap(bottomRect, background, bottom);
    painter.drawPixmap(centerRect, background, center);
}

Widget::~Widget()
{
    delete ui;
}
